package com.sale.plugins;

import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.SpringContextLoader;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.tools.DataMap;
import com.apache.tools.StrUtil;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author admin
 * 获取中签信息概况
 */
    public class SaleFindAllocationInfoPluginImpl implements CustomMethodPlugin {
    private Logger log = Logger.getLogger(getClass());
    @Override
    public Object beforeInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        String userId = (String) param.get("trsNumbers.w_userId");
        String proNo = (String) (param.get("trsNumbers.wl_proNo") == null ?
                "" :
                param.get("trsNumbers.wl_proNo"));
        String proName = (String) (param.get("trsNumbers.wl_proName") == null ?
                "" :
                param.get("trsNumbers.wl_proName"));
        String isSelect = (String) (param.get("trsNumbers.w_isSelect") == null ?
                "" :
                param.get("trsNumbers.w_isSelect"));
        StringBuffer dyncSql = new StringBuffer(
                "select r.pro_no,r.pro_name,sum(nums) nums,r.is_select from (");
        dyncSql.append("select tn.pro_no,tn.pro_name,tn.sale_id,");
        dyncSql.append(
                "(select count(t1.is_select) from trs_numbers t1 where t1.is_select='T' and t1.user_id='"
                        + userId + "' and t1.pro_no=tn.pro_no) is_select,");
        dyncSql.append("(select count(t2.user_id) from trs_numbers t2 where t2.user_id='" + userId
                + "' and t2.pro_no=tn.pro_no) nums,");
        dyncSql.append(
                "(select ts.sale_status from trs_sale ts where ts.sale_id=tn.sale_id) sale_status");
        dyncSql.append(" from trs_numbers tn where tn.user_id='" + userId
                + "'  group by tn.pro_no,tn.pro_name,tn.sale_id");
        dyncSql.append(" ) r where r.nums>0 and r.sale_status >=60");
        if (StrUtil.isNotEmpty(proNo)) {
            dyncSql.append(" and r.pro_no like'%" + proNo + "%'");
        }
        if (StrUtil.isNotEmpty(proName)) {
            dyncSql.append(" and r.pro_name like'%" + proName + "%'");
        }
/*		if (StrUtil.isNotEmpty(isSelect)) {
			dyncSql.append(" and r.is_select ='" + isSelect + "'");
		}
*/
        dyncSql.append(" group by r.pro_no,r.pro_name,r.is_select");
        param.put(CustomMethodPlugin.pluginSql,
                dyncSql.toString());//put("plugin_execSql", dyncSql.toString());
        //		ProxyManager netProxyManager = (ProxyManager) SpringContextLoader.getBean("netProxyManager");
        //		ParamsVo params = new ParamsVo();
        //		params.setParams("sqlKeyId", "ByObjInfo");
        //		params.setKey("publicForSql");
        //		params.setParams("dyncSql", dyncSql);
        //		System.out.println("配号-查询中签信息SQL=>" + dyncSql);
        //		ResultEntity dymicSqlParamResult = (ResultEntity) netProxyManager.doInvoke(params);
        //		Map d = new HashMap();
        //		return dymicSqlParamResult;
        log.info(dyncSql.toString());
        //	param.put("numbersInfo", numbersInfo);
        //return numbersInfo;

        Map<String, String> result = new HashMap<String, String>();
        result.put(CustomMethodPlugin.pluginSql, dyncSql.toString());//3、定义插件类型：自定义执行SQL类型
        return result;
        //throw new BusinessException(numbersInfo);
    }

    @Override
    public Object doInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * @return
     */
    @SuppressWarnings({ "unused", "rawtypes", "unchecked" })
    private List<DataMap> getNumbersInfo(String dyncSql) {
        ProxyManager netProxyManager = (ProxyManager) SpringContextLoader
                .getBean("netProxyManager");
        ParamsVo params = new ParamsVo();
        params.setParams("sqlKeyId", "ByObjInfo");
        params.setKey("publicForSql");
        params.setParams("dyncSql", dyncSql);
        System.out.println("配号-查询中签信息SQL=>" + dyncSql);
        ResultEntity dymicSqlParamResult = (ResultEntity) netProxyManager.doInvoke(params);
        List<DataMap> list = (List) dymicSqlParamResult.getEntity();
        return list;
    }

}
